package com.yanqu.road.dao.impl.activity.stronghold;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdTradeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class StrongholdTradeDaoImpl extends TempDataObjectDao<StrongholdTradeData> {
    public static String getStrongholdTradeDataSql = "select * from t_u_activity_stronghold_trade where activityId=?";
    public Map<Long,StrongholdTradeData> getStrongholdTradeData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, StrongholdTradeData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getStrongholdTradeDataSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdTradeData strongholdTradeData = parse(rs);
                dataMap.put(strongholdTradeData.getUserId(), strongholdTradeData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateStrongholdTradeDataSql = "update t_u_activity_stronghold_trade set addressId=?,tradeTime=?,reward=?,isBack = ? where activityId=? and userId=? and unionId=?";
    @Override
    protected boolean updateData(StrongholdTradeData strongholdTradeData) {
        strongholdTradeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdTradeData.getAddressId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdTradeData.getTradeTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdTradeData.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, strongholdTradeData.isBack()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdTradeData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdTradeData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdTradeData.getUnionId()));
        if (getDb().executeNoneQuery(updateStrongholdTradeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdTradeDataSql = "insert into t_u_activity_stronghold_trade(activityId,userId,unionId,addressId,tradeTime,reward,isBack) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(StrongholdTradeData strongholdTradeData) {
        strongholdTradeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdTradeData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdTradeData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdTradeData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdTradeData.getAddressId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdTradeData.getTradeTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdTradeData.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, strongholdTradeData.isBack()));
        if (getDb().executeNoneQuery(insertStrongholdTradeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdTradeData parseData(ResultSet rs) throws SQLException {StrongholdTradeData strongholdTradeData = new StrongholdTradeData();
        strongholdTradeData.setActivityId(rs.getInt("activityId"));
        strongholdTradeData.setUserId(rs.getLong("userId"));
        strongholdTradeData.setUnionId(rs.getString("unionId"));
        strongholdTradeData.setAddressId(rs.getInt("addressId"));
        strongholdTradeData.setTradeTime(rs.getLong("tradeTime"));
        strongholdTradeData.setReward(rs.getString("reward"));
        strongholdTradeData.setBack(rs.getBoolean("isBack"));
        return strongholdTradeData;
    }
}
